<template>
  <div class="app-container">
    <el-col :span="6">
      <el-card class="member_info">
        <el-form :label-position="labelPosition" label-width="70px" :model="memberFrom">
          <el-form-item label="姓名">
            {{memberFrom.memberName}}
          </el-form-item>
          <el-form-item label="部门名称">
            {{memberFrom.deptName}}
          </el-form-item>
          <el-row>
            <el-col :span="8">
              <el-form-item label="性别">
                {{memberFrom.sex}}
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="年龄">
                {{memberFrom.age}}
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="学历">
            {{memberFrom.edu}}
          </el-form-item>
          <el-form-item label="毕业学校">
            {{memberFrom.school}}
          </el-form-item>
          <el-form-item label="身高">
            {{memberFrom.height}}
          </el-form-item>
          <el-form-item label="上班状态">
            {{memberFrom.status}}
          </el-form-item>
          <el-form-item label="个人简介">
            {{memberFrom.brief}}
          </el-form-item>
          <el-row>
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              @click="memberupdate"
            >修改</el-button>
          </el-row>
        </el-form>
      </el-card>
    </el-col>

    <el-col :span="18">
      <el-card >
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px"
                 style="display: flex; justify-content: flex-end;">
          <el-form-item label="请假时间" prop="leaveTime">
            <el-date-picker
              v-model="queryParams.dateTime"
              type="datetimerange"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 300px">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button
              type="danger"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-form-item>

        </el-form>

        <el-row :gutter="10" class="mb8">

        </el-row>

        <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
<!--          <el-table-column type="selection" width="55" align="center" />-->
          <el-table-column label="姓名" align="center" prop="memberName" />
          <el-table-column label="标题" align="center" prop="title" show-overflow-tooltip />
<!--          <el-table-column label="缘由" align="center" prop="reason" />-->
          <el-table-column label="开始时间" align="center" prop="leaveTime" />
          <el-table-column label="返回时间" align="center" prop="backTime" />
          <el-table-column label="审批状态" align="center" prop="state" />
<!--          <el-table-column label="备注" align="center" prop="notes" />-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-more"
                @click="handleUpdate(scope.row)"
              >详情</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

      </el-card>
    </el-col>

    <el-dialog :title="membertitle" :visible.sync="openMember" width="600px" append-to-body center>
      <el-form :label-position="labelPosition" label-width="70px" :model="memberEdit">
        <el-form-item label="姓名">
          <el-input v-model="memberEdit.memberName"></el-input>
        </el-form-item>
        <el-form-item label="部门名称">
          <el-input v-model="memberEdit.deptName" :disabled="true"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="性别">
              <el-select v-model="memberEdit.sex" clearable placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="年龄">
              <el-input v-model="memberEdit.age"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="学历">
          <el-input v-model="memberEdit.edu"></el-input>
        </el-form-item>
        <el-form-item label="毕业学校">
          <el-input v-model="memberEdit.school"></el-input>
        </el-form-item>
        <el-form-item label="身高">
          <el-input-number size="mini" v-model="memberEdit.height"></el-input-number>cm
        </el-form-item>
        <el-form-item label="上班状态">
          <el-select v-model="memberEdit.dutyStatus" clearable placeholder="请选择">
            <el-option
              v-for="item in workStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="个人简介">
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="memberEdit.brief"
            maxlength="200"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitMemberEdit">确 定</el-button>
        <el-button @click="cancelmember">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改员工请假对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入" style="width: 280px"/>
        </el-form-item>
        <el-form-item label="缘由" prop="reason">
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="form.reason"
            maxlength="200"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="leaveTime">
          <el-date-picker
            v-model="form.leaveTime"
            type="datetime"
            placeholder="选择日期"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="返回时间" prop="backTime">
          <el-date-picker
            v-model="form.backTime"
            type="datetime"
            placeholder="选择日期"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="form.notes"
            maxlength="200"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listRecord,
    getRecord,
    delRecord,
    addRecord,
    updateRecord,
    getOneMemberInfo, getOneSelfMemberList
  } from "@/api/business/leaveRecord";
  import {updateMember} from "@/api/business/memberInfo";

  export default {
    name: "Record",
    data() {
      return {
        membertitle:'员工个人信息',
        options: [{
          value: '0',
          label: '男'
        }, {
          value: '1',
          label: '女'
        }, {
          value: '2',
          label: '未知'
        }],
        workStatus: [{
          value: 'WORKING',
          label: '工作中'
        }, {
          value: 'ASK_LEAVE',
          label: '请假'
        }],
        memberFrom:{},
        memberEdit:{},
        labelPosition:'right',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 员工请假表格数据
        recordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openMember:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          reason: null,
          dateTime:null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        user:{},
      };
    },
    created() {
      this.getList();
      this.getOneMember();
      this.user = this.$store.state.user;
    },
    methods: {
      /** 查询员工请假列表 */
      getList() {
        this.loading = true;
        if(this.queryParams.dateTime != null) {
          const time = this.queryParams.dateTime;
          this.queryParams.startTime = time[0];
          this.queryParams.endTime = time[1]
        }
        this.queryParams.userId = this.$store.state.user.id;
        getOneSelfMemberList(this.queryParams).then(response => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          memberId: null,
          title: null,
          reason: null,
          leaveTime: null,
          backTime: null,
          state: null,
          notes: null,
          createUserId: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.memberId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加员工请假";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const memberId = row.leaveId || this.ids
        getRecord(memberId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "员工请假详情";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.memberId = this.memberFrom.memberId;
            if (this.form.leaveId != null) {
              updateRecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const memberIds = row.memberId || this.ids;
        this.$modal.confirm('是否确认删除员工请假编号为"' + memberIds + '"的数据项？').then(function() {
          return delRecord(memberIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/record/export', {
          ...this.queryParams
        }, `record_${new Date().getTime()}.xlsx`)
      },
      getOneMember(){
        const query = {
          userId: this.$store.state.user.id
        }
        getOneMemberInfo(query).then(response=>{
          this.memberFrom = response.data
          if(this.memberFrom.sex == 0){
            this.memberFrom.sex = '男'
          }else if(this.memberFrom == 1){
            this.memberFrom.sex = '女'
          }else {
            this.memberFrom.sex = '未知'
          }
        })
      },
      memberupdate(){
        this.openMember = true;
        const query = {
          userId: this.$store.state.user.id
        }
        getOneMemberInfo(query).then(response=>{
          this.memberEdit = response.data
        })
      },
      cancelmember(){
        this.openMember = false;
      },
      submitMemberEdit() {
        this.memberEdit.status = null;
        updateMember(this.memberEdit).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openMember = false;
          this.getOneMember();
        })
      }
    }
  };
</script>

<style>
  .member_info{
    height: 600px;
  }
</style>
